#include "mainwidget.h"
#include "ui_mainwidget.h"
#include<buyer.h>
#include<QTextBrowser>
#include<QMessageBox>
#include<QTcpServer>
#define  prise_per_k  1.5    /*每公里票价*/
mainWidget::mainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mainWidget)
{

    Station[0].setdistance(110);    //车站初始化
    Station[0].setsname("A");
    Station[1].setdistance(120);
    Station[1].setsname("B");
    Station[2].setdistance(130);
    Station[2].setsname("C");
    Station[3].setdistance(140);
    Station[3].setsname("D");
    Station[4].setdistance(50);
    Station[4].setsname("E");
    Station[5].setdistance(60);
    Station[5].setsname("F");
    Station[6].setdistance(70);
    Station[6].setsname("G");
    Station[7].setdistance(90);
    Station[7].setsname("H");
    Station[8].setdistance(80);
    Station[8].setsname("I");
    Station[9].setdistance(60);
    Station[9].setsname("J");



    ui->setupUi(this);
    this->setWindowTitle("火车票购票系统服务端");
    this->serve = new QTcpServer(this);
    this->serve->listen(QHostAddress::Any,0614);
    connect(serve,&QTcpServer::newConnection,
            [=]()
    {
        mysocket = serve->nextPendingConnection();
        QString ip = mysocket->peerAddress().toString();
        qint16 port = mysocket->peerPort();
        ui->show->setText(QString("[%1:%2]").arg(ip).arg(port));
        connect(mysocket,&Tcpsocket::readyRead,
                [=]()
        {
             QByteArray array = mysocket->readAll();
             ui->show->setText(array);
        }
                );

    }
            );



}

mainWidget::~mainWidget()
{
    delete ui;
}

void mainWidget::searching()                 //查询余票
{   QString s_station;    /*起点站*/
    QString e_station;    /*终点站*/
    s_station = ui->Start->currentText();
    e_station = ui->End->currentText();
    int min_ticket;
    bool error = false;         /*错误标记*/
    for(int i=0;i<10;i++)
     {
        if(s_station.compare(e_station)==0)
        {
            error = true;           /*车站选择错误*/
            ui->show->setText("路线选择有误，请重新选择路线！");
            break;
        }

        if(Station[i].getsname().compare(s_station) == 0)
           {
                min_ticket = Station[i].getremained();
                for(int j=i;Station[j].getsname().compare(e_station)!=0;j++)
                   {
                         if(j == 9)        /*车站填写顺序错误*/
                          {
                          error = true;           /*车站选择错误*/
                          break;
                          }
                         if(Station[j].getremained()<min_ticket)
                               {
                                  min_ticket=Station[j].getremained();
                               }


                  }
                if(error == false)     /*未发生错误*/
                {
                    double ticket_prise = this->get_sum_prise(s_station,e_station);
                  ui->show->setText(QString("%1-%2剩余车票：%3;票价：%4").arg(s_station).arg(e_station).arg(min_ticket).arg(ticket_prise)); /*输出剩余车票*/
                   break;
                }
                else{
                    ui->show->setText("路线选择有误，请重新选择路线！");
                    break;
                }
           }


     }
    }




void mainWidget::buying()                   //购票
{
     QString str = ui->Name->text();
     mysocket->write(str.toUtf8().data());
     for(int i=0;i<100;i++)
     {
         if(ui->Name->text().isEmpty()&&ui->Identify->text().isEmpty())
         {
             ui->show->setText("请输入姓名或身份证");
             break;
         }

                if(buyerinf[i].gettorf() == false)
        {           QString buyername,buyerIden,s_station,e_station;
                    double Prise;
                    buyername = ui->Name->text();
                    buyerIden = ui->Identify->text();
                    s_station = ui->Start->currentText();
                    e_station = ui->End->currentText();
                    Prise = this->get_sum_prise(s_station,e_station);
                    int ret = QMessageBox::question(this,"提示","确认购票");

                    if(ret == QMessageBox::Yes)
                 {
                        buyerinf[i].getinformation(buyername,buyerIden,s_station,e_station,Prise);/*将键盘中输入的姓名，身份证等信息输入到信息数组中*/
                        buyerinf[i].settrue();                       /*标记买票*/
                        this->settickets(s_station,e_station);       /*票数-1*/
                        ui->show->setText(QString("购票成功！ 路线：%1-%2").arg(s_station).arg(e_station));               /*文本框中输出购票完成提示*/
                        break;
                 }
                 else{
                     break;
                 }

        }
    }

}

void mainWidget::cancell(QString nameoriden)              //退票
    {
                     for(int i=0;i<100;i++)
               {
                      if(nameoriden.compare(buyerinf[i].getidentify())==0||nameoriden.compare(buyerinf[i].getname())==0)
                          {
                                  if(buyerinf[i].gettorf() == false)
                                   {
                                        ui->show->setText("您未购票");      /*输出未购票提示*/
                                    }


                               else{
                                   int ret = QMessageBox::question(this,"提示","确认退票");              /*对话框--是否退票*/
                                    if(ret == QMessageBox::Yes)    /*是则*/
                                    {
                                        buyerinf[i].setfalse();
                                        buyerinf[i].delete_information();   /*撤销购票信息*/
                                       this->addtickets(buyerinf[i].getstart(),buyerinf[i].getend());
                                        ui->show->setText("退票成功！");
                                             break;
                                    }
                                 }

                                  break;

                          }
                      if(i == 99)
                      {
                           ui->show->setText("对不起，您未购票。"); /*输出未购票提示*/
                      }

                }
    }

void mainWidget::seek_information(QString nameoriden)     //购票信息
{
         for(int i=0;i<100;i++)
    {
             if(ui->Name == nullptr&&ui->Identify ==nullptr)
             {
                   ui->show->setText("对不起，您未输入任何信息");
                   break;
             }
           if(nameoriden.compare(buyerinf[i].getidentify())==0||nameoriden.compare(buyerinf[i].getname())==0)
        {
                 if(buyerinf[i].gettorf() == false)
                      {
                                 ui->show->setText("对不起，您未购票");
                      }
                else
                    {
                         ui->show->setText(QString("姓名：%1  身份证：%2  出发站：%3  到达站：%4  票价：%5").arg(buyerinf[i].getname()).arg(buyerinf[i].getidentify()).arg(buyerinf[i].getstart()).arg(buyerinf[i].getend()).arg(buyerinf[i].getprise()));
                         break;
                    }
         }
        if(i == 99)
        {
             ui->show->setText("对不起，您未购票");          /*查无此人*/
           }
    }
}



void mainWidget::on_book_clicked()   //点击购票
{
    this->buying();
}

void mainWidget::on_pushButton_2_clicked()  /*点击查询余票*/
{
  this->searching();
}

void mainWidget::on_pushButton_3_clicked()  /*点击查询购票信息*/
{
    QString name1,identify1;
    name1 = ui->Name->text();
    identify1 = ui->Identify->text();


    if(!name1.isEmpty())              /*通过姓名查询购票信息*/
    this->seek_information(name1);


    else if(!identify1.isEmpty())     /*通过身份证查询购票信息*/
      this->seek_information(identify1);
}

void mainWidget::on_pushButton_4_clicked()  /*点击退票*/
{
    QString name1,identify1;
    name1 = ui->Name->text();
    identify1 = ui->Identify->text();
    if(!name1.isEmpty())
   this->cancell(name1);
    else if(!identify1.isEmpty())
        this->cancell(identify1);
}

double mainWidget::get_sum_prise(QString station1, QString station2)  /*获取票价*/
{       double sum=0;
    for(int i=0;i<10;i++)
    {
        if(Station[i].getsname().compare(station1)==0)
        {
            for(int j=i;Station[j].getsname().compare(station2)!=0;j++)
            {
                sum+=Station[j].get_distance()*prise_per_k;
            }
         break;
        }

    }
        return sum;
}

void mainWidget::settickets(QString station1, QString station2)
{
    for(int i=0;i<10;i++)
    {
        if(Station[i].getsname().compare(station1)==0)
        {
            for(int j=i;Station[j].getsname().compare(station2)!=0;j++)
            {
                  Station[j].numoticket();
            }
         break;
        }

    }
}

void mainWidget::addtickets(QString station1, QString station2)
{
    for(int i=0;i<10;i++)
    {
        if(Station[i].getsname().compare(station1)==0)
        {
            for(int j=i;Station[j].getsname().compare(station2)!=0;j++)
            {
                  Station[j].add_ticket();
            }
         break;
        }

    }
}
